#!usr/bin/env python
# -*- coding:utf-8 -*-
# user nanme: Mr.zhang
# data time : 2022/11/18   10:06 上午
# project   : get_devices.py
# IDE       : PyCharm
# email     :17600960626@163.com
import logging

from driver.conf_devices import devices
from lib.read_sql import db



class init_dev(devices):

    def __init__(self):
        self.dev = self.devlists()

    def initi_The_Device(self):
        del_sql = 'truncate table dev_table'
        db.deleteDB(del_sql)
        sql_list = []
        sql1 = 'SELECT devices FROM dev_table'
        sql = db.selectDb(sql1)

        for i in sql[1]:
            sql_list.append(i[0])
        # 数据库中不存在就插入一条
        b = [x for x in self.dev if x not in sql_list]
        c = [y for y in self.dev if y in sql_list]
        d = [z for z in sql_list if z not in self.dev]

        for i in b:
            sql2 = 'INSERT IGNORE INTO dev_table (devices,code) VALUES("%s","0")' % i
            db.insertDB(sql2)
        logging.warning('入库的新设备ID是：%s' % b)

        for j in c:
            sql3 = "UPDATE dev_table SET code = 0 WHERE (devices='%s' OR code=2) AND (code=1)" % j
            db.updateDb(sql3)
        logging.warning('重置设备状态为空闲，设备ID是：%s' % c)

        for h in d:
            sql4 = "SELECT * FROM dev_table WHERE devices='%s' and code!=1"%h
            db.selectDb(sql4)
        logging.warning('库存表中的设备状态为空闲，设备ID是：%s' %d)

